package com.itboye.services.ucenter;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itboye.mybatis.helper.ByPageInfoHelper;
import com.itboye.mybatis.mapper.IBaseMapper;
import com.itboye.services.ucenter.interfaces.IBaseService;
import com.itboye.system.exception.ByServiceException;
import com.itboye.system.helper.ByStringHelper;

import java.util.HashMap;
import java.util.List;

/**
 * Created by hebidu on 16/7/19.
 */
public abstract class BaseService<T> implements IBaseService<T> {

    public abstract IBaseMapper<T> getMapper();



    public int update(HashMap params)throws ByServiceException {
        return getMapper().update(params);
    }

    public T getInfo(int id) throws ByServiceException {
        return getMapper().getInfo(id);
    }

    public T getInfoBy(HashMap params, String fields, String orders) throws ByServiceException {
        return getMapper().getInfoBy(params,fields,orders);
    }

    public T getInfoBy(HashMap params) throws ByServiceException {
        return getMapper().getInfoBy(params);
    }

    public List<T> query(HashMap params, String fields, String orders) throws ByServiceException{
        if(ByStringHelper.isEmpty(fields)){
            params.put("fields",fields);
        }
        if(ByStringHelper.isEmpty(orders)){
            params.put("orders",orders);
        }

        return getMapper().query(params);
    }

    public ByPageInfoHelper<T> queryByPaging(HashMap params, int pageNum, int pageSize, String fields, String orders)throws ByServiceException {
        PageHelper.startPage(pageNum,pageSize);

        List<T> list =  query(params,fields,orders);
        if(list == null){
            Page<T> emptyPage = new Page<T>();
            emptyPage.setPageNum(pageNum);
            emptyPage.setPages(0);
            emptyPage.setTotal(0);
            emptyPage.setPageSize(pageSize);

            return new ByPageInfoHelper<T>(emptyPage);
        }

        Page<T> emptyPage = new Page<T>();
        return new ByPageInfoHelper<T>(((Page<T>)list));
    }

    public int insert(T model) throws ByServiceException {
        return getMapper().insert(model);
    }

    public int batchInsert(List<T> list) throws ByServiceException {
        return getMapper().batchInsert(list);
    }
}
